<?php

namespace App\Http\Controllers\Backend\Message;

use App\Http\Requests\Backend\Message\NewsMessageRequest;
use App\Models\NewsMessage;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class NewsMessageController extends Controller
{
    protected $admin_user;

    public function __construct()
    {
        $this->middleware('auth:backend');
        $this->admin_user = Auth::guard('backend')->user();
    }

    /**
     * Description:
     * Author: wjl
     * DateTime: 2019-04-10 19:35
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $news_message = new NewsMessage();
        $news_message->setConnection('backend');

        $page_size = $request->get('page_size', null);
        if($page_size) {
            $list = $news_message->orderByDesc('created_at')->paginate($page_size);
            if ($list) {
                $info = [];
                foreach ($list as $v) {
                    $info[] = [
                        'id' => $v->id,
                        'content' => $v->content,
                        "is_show" => $v->is_show,
                        "admin_user" => $v->admin_user,
                        "created_at" => $v->created_at->format('Y-m-d H:i:s'),
                        "updated_at" => $v->updated_at->format('Y-m-d H:i:s'),
                    ];
                }
                return resp(['data' => $info, 'total' => $list->total()]);
            }
        } else {
            $list = $news_message->orderByDesc('created_at')->get();
            $info = [];
            foreach ($list as $v) {
                $info[] = [
                    'id' => $v->id,
                    'content' => $v->content,
                    "is_show" => $v->is_show,
                    "admin_user" => $v->admin_user,
                    "created_at" => $v->created_at,
                    "updated_at" => $v->updated_at,
                ];
            }
            return success($info);
        }
        return success($list);
    }

    /**
     * Description: 快讯详情获取
     * Author: wjl
     * DateTime: 2019-04-10 19:50
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $news_message = new NewsMessage();
        $news_message->setConnection('backend');

        $messageInfo = $news_message->where('id', $id)->first()->toArray();
        return empty($messageInfo) ? failed('当前信息不存在') : success($messageInfo);
    }

    /**
     * Description:更新快讯信息
     * Author: wjl
     * DateTime: 2019-04-10 20:01
     * @param NewsMessageRequest $request
     * @param $id
     * @return mixed
     */
    public function update(NewsMessageRequest $request, $id)
    {
        $updateData = $this->extractInputFromRules($request, $request->rules());
        $updateData['admin_user'] = $this->admin_user->account;
        
        $news_message = new NewsMessage();
        $info = $news_message->where('id', $id)->update($updateData);

        return $info ? updated('') : failed('更新失败');
    }

    /**
     * Description: 添加快讯信息
     * Author: wjl
     * DateTime: 2019-04-10 20:05
     * @param NewsMessageRequest $request
     * @return mixed
     */
    public function store(NewsMessageRequest $request)
    {
        $updateData = $request->only(['content']);
        $news_message = new NewsMessage();
        $news_message->content = $updateData['content'];
        $news_message->admin_user = $this->admin_user->account;
        $info = $news_message->save();
        return $info ? stored('') : failed('添加失败');
    }

    /**
     * Description:删除消息模板
     * Author: Sam
     * DateTime: 2019/1/12 10:08
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $news_message = new NewsMessage();
        $news_message->destroy($id);
        return success([]);
    }
}
